/*
 *
  $('#treeview').treeview({root:id,url:'admin/menu.php?action=get_menu_list',target:'right',click:function(url){
        $('#right').attr('src',url);
  }});
*/
(function($){
    $.fn.extend({
        
        treeview : function(settings){
            settings = $.extend(settings);
            url = settings.url;
            root = settings.root ? settings.root : 0;
            click = settings.click;
            this.html('');
            this.addClass('treeview').loadtree(root);
            this.find('li').prepareBranches();
            return this;
        },
        togglesub : function()
        {
            this.find('ul').remove();
            if(this.filter('.expandable').size()>0)
            {
                return this;
            }
            var id = this.attr('id').substr(5);
            this.loadtree(id);
            return this;
        },
        loadtree : function(menuid){
            var html = '';
            var id= this.attr('id');
            $.post(url,{menuid:menuid},function(msg){
                var spanclass='';
                var liclass='';
                var url='';
                eval("var data="+msg);
                if(data.length>0)
                {
                    html += '<ul>';
                    for(i=0;i<data.length;i++)
                    {
                        if(data[i]['isfolder']==1)
                        {
                            spanclass = 'class=\'folder\'';
                            if(data[i]['isopen']!=1)
                            {
                                liclass = 'class=\'closed\'';
                            }
                        }
                        else
                        {
                            url = "url='"+data[i]['url']+"'";
                            spanclass = 'class=\'file\'';
                        }
                        var span = "<span "+url+" "+spanclass+">"+data[i]['name']+"</span>";
                        html += "<li id='menu_"+data[i]['id']+"' "+liclass+">"+span+"</li>";
                    }
                    html += '</ul>';
                    $('#'+id).append(html).find('li').prepareBranches({});
                }
            });
            return this;
        },
        //调换class
        swapClass : function(c1,c2)
        {
            var lic1 = $(this).filter('.'+c1);
            var lic2 = $(this).filter('.'+c2);
            lic1.removeClass(c1).addClass(c2);
            lic2.removeClass(c2).addClass(c1);
            return this;
        },
        //初始化分支
        prepareBranches : function()
        {
            this.filter(":last-child:not(ul)").addClass(CLASSES.last);
            this.filter(":has(.folder)").addClass(CLASSES.expandable).prepend("<div class='"+CLASSES.hitarea+' '+CLASSES.expandableHitarea +"'></div>")
                .find('div,span').click(function(){
                    toggler(this);
                });
            this.filter(":last-child:not(ul):has(.folder)").addClass(CLASSES.lastExpandable)
                .find('.'+CLASSES.hitarea).addClass(CLASSES.lastExpandableHitarea);
            this.filter(("." + CLASSES.closed) + ":not(." + CLASSES.open + ")").find(">ul").hide();
            //this.find('a').attr('target',target);
            this.filter(":has(.file)").find('span').click(function(){
                click($(this).attr('url'));
            });
            toggler(this.filter(":has(.folder):not(.closed)").find('span'));
            //文件夹打开关闭
            function toggler(obj) {
                $(obj).parent().swapClass(CLASSES.expandable,CLASSES.collapsable).swapClass(CLASSES.lastCollapsable,CLASSES.lastExpandable)
                .find(">."+CLASSES.hitarea).swapClass(CLASSES.expandableHitarea,CLASSES.collapsableHitarea)
                                  .swapClass( CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea )
                .end()
                .each(function(){$(this).togglesub()});
            }
            return this;
        }
    });

    //预定义class
    var url = '';
    var click = function(){};
    var root = '';
    var CLASSES = $.fn.treeview.classes = {
		open: "open",
		closed: "closed",
		expandable: "expandable",
		expandableHitarea: "expandable-hitarea",
		lastExpandableHitarea: "lastExpandable-hitarea",
		collapsable: "collapsable",
		collapsableHitarea: "collapsable-hitarea",
		lastCollapsableHitarea: "lastCollapsable-hitarea",
		lastCollapsable: "lastCollapsable",
		lastExpandable: "lastExpandable",
		last: "last",
		hitarea: "hitarea"
	};
})($);